package com.xgp.qq;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;

//代码生成器
public class Code {
    public static void main(String[] args) {
        //构建一个代码生成器对象
        AutoGenerator mpg = new AutoGenerator();
        //配置策略

        //1、全局配置
        GlobalConfig gc = new GlobalConfig();
        //输出目录
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("咸鱼");
        gc.setOpen(false);
        gc.setFileOverride(false);  //是否覆盖原来生成的
        gc.setServiceName("%sService");//去Service的i前缀
        gc.setIdType(IdType.ID_WORKER);     //主键策略
        gc.setDateType(DateType.ONLY_DATE); //日期类型
        gc.setSwagger2(true);           //配置Swagger文档
        mpg.setGlobalConfig(gc);

        //2、数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
//        dsc.setUrl("jdbc:mysql://112.126.97.63:3306/campussong?useSSL=false&useUnicode=true&character=utf-8&serverTimezone=Asia/Shanghai");
        dsc.setUrl("jdbc:mysql://112.126.97.63:3306/micro_chat?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=round");
        dsc.setUsername("root");
        dsc.setPassword("123456");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setDbType(DbType.MYSQL);

        mpg.setDataSource(dsc);

        //3、包的配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName("microChat");
        pc.setParent("com.xgp");
        pc.setEntity("model");
        pc.setMapper("mapper");
        pc.setService("service");
        pc.setController("controller");

        mpg.setPackageInfo(pc);

        //4、策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setInclude("user");//需要映射的表名
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);    //自动Lombok

//        逻辑删除的名字
        strategy.setLogicDeleteFieldName("deteled");

//        自动填充配置
        TableFill gmtCreate = new TableFill("gmt_create", FieldFill.INSERT);
        TableFill gmtModified = new TableFill("gmt_modified", FieldFill.INSERT_UPDATE);
//
        ArrayList<TableFill> tablefills = new ArrayList<>();
        tablefills.add(gmtCreate);
        tablefills.add(gmtModified);
        strategy.setTableFillList(tablefills);

//        乐观锁的配置
        strategy.setVersionFieldName("version");

//        开启驼峰命名
        strategy.setRestControllerStyle(true);
        strategy.setControllerMappingHyphenStyle(true);//localhost:8080/hello_id_2
        mpg.setStrategy(strategy);

//        TemplateConfig templateConfig = new TemplateConfig();
//        mpg.setTemplate(templateConfig);
        System.out.println(mpg);
        mpg.execute();  //执行
    }
}
